package com.hz.qs.controller;

import com.hz.qs.model.YiCustomerDetail;
import com.hz.qs.model.YiQueues;
import com.hz.qs.model.YiReqChange;
import com.hz.qs.model.vo.CustomerRequest;
import com.hz.qs.service.CoreService;
import com.hz.qs.service.CustomerService;
import com.hz.qs.utils.RestResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 客户列表相关页面controller
 *
 * @author yejy
 * @date 2020/10/29 16:04
 */
@Slf4j
@RestController
@RequestMapping("customer/")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CoreService coreService;

    @ApiOperation("02 获取客户列表页面数据")
    @PostMapping("getCustomerList")
    public RestResult getCustomerList(@RequestBody CustomerRequest customerRequest) {
        return customerService.getCustomerList(customerRequest);
    }


    @ApiOperation("02 刷新时调用，用于刷新队列")
    @PostMapping("refreshQueues")
    public RestResult refreshQueues(@RequestBody CustomerRequest customerRequest) {
        try {
            String jgsId = Optional.ofNullable(customerRequest.getHealthMasterNo()).orElse("");
            if ("".equals(jgsId)) {
                coreService.optQueuesData2(customerRequest);
            }
            return RestResult.successResult(null);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.failResult("刷新繁忙！请稍后再试！！", 502);
        }
    }


    @ApiOperation("03 根据客户ID获取检查项目数据")
    @GetMapping("getCheckProjectsByCustomerId")
    public RestResult getCheckProjectsByCustomerId(@RequestParam String customerId,@RequestParam String currentDate) {
        List<YiQueues> projects = customerService.getCheckProjectsByCustomerId(customerId,currentDate);
        return RestResult.successResult(projects);
    }


    @ApiOperation("03 申请调整")
    @PostMapping("addRequestChange")
    public RestResult addRequestChange(@RequestBody YiReqChange yiReqChange) {
        try {
            Integer integer = customerService.addRequestChange(yiReqChange);
            return RestResult.successResult(integer);
        } catch (Exception e) {
            log.error(e.getMessage());
            return RestResult.failResult("调整申请失败！", 500);
        }
    }


    @ApiOperation("03 放弃检查")
    @GetMapping("cancelCheck")
    public RestResult cancelCheck(@RequestParam String customerId, @RequestParam String id, @RequestParam String currentDate) {
        try {
            Integer integer = customerService.cancelCheck(customerId, id, currentDate);
            if (integer == -2) {
                return RestResult.failResult("该项目状态不允许放弃检查！", 4002);
            }
            return RestResult.successResult(integer);
        } catch (Exception e) {
            log.error(e.getMessage());
            return RestResult.failResult("放弃检查失败！", 500);
        }
    }


    @ApiOperation("06 客户详情展示")
    @GetMapping("getCustomerDetail")
    public RestResult getCustomerDetail(@RequestParam String customerId, @RequestParam String currentDate) {
        return customerService.getCustomerDetail(customerId, currentDate);
    }


    @ApiOperation("07 客户详情编辑")
    @PostMapping("editCustomerDetail")
    public RestResult editCustomerDetail(@RequestBody YiCustomerDetail customerDetail) {
        try {
            Integer integer = customerService.editCustomerDetail(customerDetail);
            return RestResult.successResult(integer);
        } catch (Exception e) {
            log.error(e.getMessage());
            return RestResult.failResult("客户详情编辑失败！", 500);
        }
    }


    @ApiOperation("21 调整申请展示列表")
    @GetMapping("getRequestChangeList")
    public RestResult getRequestChangeList() {
        return customerService.getRequestChangeList();
    }


    @ApiOperation("22 实时刷新叫号信息")
    @GetMapping("loopTips")
    public RestResult loopTips(@RequestParam String jgsId, @RequestParam String currentDate) {
        return customerService.loopTips(jgsId, currentDate);
    }

    @ApiOperation("22 确认叫号信息")
    @GetMapping("confirmCall")
    public RestResult confirmCall(@RequestParam String id) {
        try {
            return customerService.confirmCall(id);
        } catch (Exception e) {
            log.error(e.getMessage());
            return RestResult.failResult("确认叫号信息失败！", 500);
        }
    }

    @ApiOperation("上移下移(type=up/down)")
    @GetMapping("upOrDown")
    public RestResult upOrDown(@RequestParam String id, @RequestParam String currentDate, @RequestParam String type) {
        try {
            return customerService.UpOrDown2(id, type, currentDate);
        } catch (Exception e) {
            log.error(e.getMessage());
            return RestResult.failResult("上移下移失败！", 500);
        }
    }

    @ApiOperation("上移下移-队列的客户(type=up/down)")
    @GetMapping("upOrDownForQueueCustomer")
    public RestResult upOrDownForQueueCustomer(@RequestParam String queueId,@RequestParam String customerId, @RequestParam String currentDate, @RequestParam String type) {
        try {
            return customerService.upOrDownForQueueCustomer(queueId,customerId, type, currentDate);
        } catch (Exception e) {
            log.error(e.getMessage());
            return RestResult.failResult("上移下移失败！", 500);
        }
    }

}
